package com.dalin.platform.loa.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.loa.service.LoaService;
import com.dalin.platform.loa.vo.LoaOrganTreeVo;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-07                                                     *
 * @modDate: 2010-06-07                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class OrganizationAddModDelAction extends StrutsTwoAction{
    private LoaService loaService; // 
    private String strTree;
	private Long[] delIdArray;





    public String execute()throws Exception {
        super.printInfo("", "===== this action begin");
        System.out.println("strTree=="+strTree);
        try {
        	JSONArray ja = JSONArray.fromObject(strTree);
            List<LoaOrganTreeVo> treeList = getListFromJSONArray(ja);
            loaService.addUpDelOrgans(treeList, delIdArray);
            setResContent(true, "");
        } catch (Exception e) {
        	e.printStackTrace();
        }
        super.printInfo("", "===== this action end");
        return null;
    }

    
    private List<LoaOrganTreeVo> getListFromJSONArray(JSONArray ja) {
		List<LoaOrganTreeVo> rtnList = new ArrayList<LoaOrganTreeVo>();
		Long projectId = getCurrentUser().getProjectId();
		JSONObject jo = null;
		LoaOrganTreeVo treeVo = null;
		for (int i = 0; i < ja.size(); i++) {
			treeVo = new LoaOrganTreeVo();
			jo = ja.getJSONObject(i);
			treeVo.setId(getNullString(jo,"id")); // 结点编码
			treeVo.setText(getNullString(jo,"text")); // 结点名称
			treeVo.setOrganType(getNullString(jo,"organType")); // 组织类型
			treeVo.setBelongAreaCode(getNullString(jo,"belongAreaCode")); // 所属区域代码
			treeVo.setParentId(getNullString(jo,"parentId")); // 父结点ID
			treeVo.setRemark(getNullString(jo,"remark")); // 备注
			treeVo.setProjectId(projectId);
			rtnList.add(treeVo);
		}
		return rtnList;
	}
    
    private String getNullString(JSONObject jo, String key) {
    	String value = null;
    	try {
    		value = jo.getString(key);
    	} catch (JSONException E) {
    	}
    	return value;
    }
    public LoaService getLoaService() {
        return loaService;
    }

    public void setLoaService(LoaService loaService) {
        this.loaService = loaService;
    }
	public String getStrTree() {
		return strTree;
	}
	public void setStrTree(String strTree) {
		this.strTree = strTree;
	}
	public Long[] getDelIdArray() {
		return delIdArray;
	}

	public void setDelIdArray(Long[] delIdArray) {
		this.delIdArray = delIdArray;
	}
}
